package com.android.vspice;

import java.io.IOException;
import java.nio.ByteBuffer;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback, Camera.PreviewCallback {
	private int mWidth, mHeight;
	private Camera mCamera = null;
	private SurfaceHolder mHolder;
	private boolean mTake = false;
	
	public CameraPreview(Context context, int width, int height) {
		super(context);
		mWidth = width;		mHeight = height;
		
		mHolder = getHolder();
		mHolder.addCallback(this);
		mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub
		try {
			initCamera(holder);
		} catch(InterruptedException e) {
			e.printStackTrace();
			return;
		}
		
		Camera.Parameters param = mCamera.getParameters();
		param.setPreviewSize(mWidth, mHeight);
		param.setPictureSize(mWidth, mHeight);
		param.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
		mCamera.setParameters(param);
		
		mCamera.startPreview();
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		releaseCamera();
	}

	@Override
	public void onPreviewFrame(byte[] data, Camera camera) {
		if(mTake) {
			mCamera.stopPreview();
			byte[] rgb = new byte[mWidth * mHeight];
			VSpiceJNI.yuvToRGB(data, rgb, mWidth, mHeight, true);
		}
	}
	
	public void setFocus() {
		if(mCamera != null)
			mCamera.autoFocus(null);
	}
	
	public void setPreviewSize(int width, int height){
		mWidth = width;
		mHeight = height;
	}
	
	private void initCamera(SurfaceHolder holder) throws InterruptedException {
		if (mCamera == null) {
			
			int i = 0;
			while (i++ < 5) {
				try {
					mCamera = Camera.open();
					break;
				} catch (RuntimeException e) {
					Thread.sleep(200);
				}
			}
			try {
				mCamera.setPreviewDisplay(holder);
				mCamera.setPreviewCallback(this);
			} catch (IOException exception) {
				mCamera.release();
				mCamera = null;

			} catch (RuntimeException e) {
				Log.e("VisionSpice", "RuntimeException", e);
			}
		}
	}

	private void releaseCamera() {
		if (mCamera != null) {
			
			mCamera.stopPreview();
			mCamera.release();
		}
		
		mCamera = null;
	}
	
	public boolean takePicture() {
		/*
		if(mCamera != null) {
			if(!mTake)
				mCamera.takePicture(null, raw, jpeg);
			else
				mCamera.startPreview();
			
			mTake = !mTake;
		}*/
		if(mTake) {
			mCamera.startPreview();
		} else {
			
		}
		mTake = !mTake;		
		
		return mTake;
	}
	
	public void onPause() {
		releaseCamera();
	}
	
	public void onResume() {
		
	}
	
	private PictureCallback raw = new PictureCallback() {
		@Override
		public void onPictureTaken(byte[] data, Camera camera) {
			//VSpiceJNI.test();			
			//byte[] grey = new byte[mWidth * mHeight];
			
		}
	};	
	
	private PictureCallback jpeg = new PictureCallback() {
		@Override
		public void onPictureTaken(byte[] data, Camera camera) {
			VSpiceJNI.test();			
			//byte[] grey = new byte[mWidth * mHeight];
			Bitmap bitCircuit = BitmapFactory.decodeByteArray(data, 0, data.length);
			ByteBuffer buf = ByteBuffer.allocateDirect(bitCircuit.getRowBytes() * bitCircuit.getHeight());
			bitCircuit.copyPixelsToBuffer(buf);
		}
	};	
}
